<?php
/**
 * Edit my data
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       Zend_Controller_Action
 * @category   Module
 * @package    User
 * @subpackage Controller
 * @todo
 */
class User_AppController extends Zend_Controller_Action
{
    /**
     * Initialisation
     *
     * @return void
     */
    public function init()
    {
        $this->view->headLink()->appendStylesheet($this->view->baseUrl("/css/user/app_user.css"));
    }
    /**
     * Show user data
     *
     * @return void
     */
    public function indexAction()
    {
        $identity = Zend_Auth::getInstance()->getIdentity();

        $user = new User_Model_User();
        $data = $user->read($identity['id']);

        $this->view->firstname = $data['firstname'];
        $this->view->lastname  = $data['lastname'];
        $this->view->username  = $identity['username'];
        $this->view->role      = $data['role'];
    }
    /*
     * Edit user data
     */
    public function editAction()
    {
        $identity = Zend_Auth::getInstance()->getIdentity();

        $user = new User_Model_User();
        $formRequest = $this->_request->getPost('form');

        if($formRequest) {

            /** Set input filters and validators */ 
            $filters = array('*' => array('StringTrim','StripTags'));
            $validators = array(
                'username' => array(
                    'allowEmpty' => FALSE,
                    array('stringLength', 3, 100)),
                'firstname' => array(
                    'allowEmpty' => FALSE,
                    array('stringLength', 3, 100)),
                'lastname' => array(
                    'allowEmpty' => FALSE,
                    array('stringLength', 3, 100)
                )
            );

            $input = new Zend_Filter_Input($filters, $validators, $formRequest);

            if($input->isValid()) {
                $user->save($input->getEscaped(), $identity['id']);
                $this->_forward('index');
                return;
            } else {
                $this->view->messages = $input->getMessages();
                $errors = $input->getErrors();

                /** Set error messages for the view */
                foreach ($errors as $fieldname => $errorTyp) {

                    $this->view->{$fieldname . 'Error'} = 'error';
                }
                $data = $formRequest;
            }
        } else {
            $data = $user->read($identity['id']);
        }

        $this->view->firstname = $data['firstname'];
        $this->view->lastname  = $data['lastname'];
        $this->view->username  = $identity['username'];
        $this->view->role      = $data['role'];
    }
    /**
     * Change password
     */
    public function passwordAction()
    {
        $formRequest = $this->_request->getPost('form');

        if($formRequest) {

            /** Set input filters and validators */ 
            $filters = array('*' => array('StringTrim','StripTags'));
            $validators = array(
                'old' => array(
                    'allowEmpty' => FALSE),
                'new' => array(
                    'allowEmpty' => FALSE,
                    array('stringLength', 8, 100)),
                'repeat' => array(
                    'allowEmpty' => FALSE,
                    array('stringLength', 8, 100)
                )
            );

            $input = new Zend_Filter_Input($filters, $validators, $formRequest);

            if($input->isValid()) {

                $password = new User_Model_Password();
                if($password->check($formRequest["old"])) {

                    if($formRequest["new"] == $formRequest["repeat"]) {

                        $identity = Zend_Auth::getInstance()->getIdentity();
                        $password->change($formRequest['new'], $identity['id']);
                        $this->view->alertSuccess = "Passwort wurde geändert";

                    } else {
                        $this->view->alertError = "Passwörter stimmen nicht überein";
                        $this->view->newError = 'error';
                        $this->view->repeatError = 'error';
                    }
                } else {
                    $this->view->alertError = "Falsches Password";
                    $this->view->oldError = 'error';
                }
            } else {

                $this->view->alertError = "Mindestlänge 8 Zeichen";
            }
        }
    }
}